package com.lanou3g.dllo.baidumusic.musiclibrary.radio.radiodetail;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseFragment;
import com.lanou3g.dllo.baidumusic.main.playmusic.PlayMusicService;
import com.lanou3g.dllo.baidumusic.main.playmusic.PlayOrPause;
import com.lanou3g.dllo.baidumusic.main.playmusic.SongPlayBean;
import com.lanou3g.dllo.baidumusic.tools.PlaylistManager;
import com.lanou3g.dllo.baidumusic.tools.SingleVolley;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * Created by dllo on 16/7/2.
 */
public class EachRadioFragment extends BaseFragment {

    private ImageView eachRadioPicture;
    private TextView eachRadioSongName, eachRadioAuther;
    private CheckBox checkBox;
    private PlayMusicService.MyBinder myBinder;
    private ServiceConnection serviceConnection;


    @Override
    public int setLayout() {
        EventBus.getDefault().register(this);
        return R.layout.fragment_eachradio;
    }

    @Override
    public void initView(View view) {
        checkBox = (CheckBox) view.findViewById(R.id.eachradio_pause);
        eachRadioPicture = (ImageView) view.findViewById(R.id.eachradio_picture);
        eachRadioSongName = (TextView) view.findViewById(R.id.eachradio_songname);
        eachRadioAuther = (TextView) view.findViewById(R.id.eachradio_auther);
    }

    @Override
    public void initData() {
        checkBox.setChecked(PlaylistManager.getInstance().isPlaying);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkBox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myBinder.playPause();
                    }
                });
            }
        });

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myBinder = (PlayMusicService.MyBinder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        Intent playMusicService = new Intent(context, PlayMusicService.class);
        context.startService(playMusicService);
        context.bindService(playMusicService, serviceConnection, context.BIND_AUTO_CREATE);

    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        context.unbindService(serviceConnection);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void toEachRadio(SongPlayBean songPlayBean) {
        eachRadioSongName.setText(songPlayBean.getSonginfo().getTitle());
        eachRadioAuther.setText(songPlayBean.getSonginfo().getAuthor());
        SingleVolley.getSingleVolley(context).getImageLoader().get(songPlayBean.getSonginfo().getPic_premium(),
                ImageLoader.getImageListener(eachRadioPicture, R.mipmap.ic_launcher, R.mipmap.ic_launcher));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void controlPauseOrPlay(PlayOrPause playOrPause) {
        checkBox.setChecked(playOrPause.isPlaying());
    }
}
